package main

import (
	"github.com/xuri/excelize"
	"strconv"
	"fmt"
	"github.com/boltdb/bolt"
	"regexp"
	"strings"
	"match-spider/util/boltdb"
	"match-spider/util/overunder"
)

func overunderScore(fn func([]interface{},int)) {
	only:=",法甲,法乙,英冠,英超,英乙,英甲,德甲,德乙,西甲,西乙,意甲,意乙,苏冠,荷甲,荷乙,葡超,"
	i:=0
	boltdb.ScanTx("matchs-2017", func(tx *bolt.Tx) {

		tx.ForEach(func(name []byte, b *bolt.Bucket) error {
			b.ForEach(func(k, v []byte) error {
				content := string(v)
				s := strings.Split(content, "^")

				league := s[1]
				startAt := s[2]
				reg, _ := regexp.Compile("\\([\\S\\s]+?\\)")
				t1 := s[3]
				t2 := s[5]
				t1 = reg.ReplaceAllString(t1, "")
				t2 = reg.ReplaceAllString(t2, "")
				s1, _ := strconv.ParseFloat(s[4], 64)
				s2, _ := strconv.ParseFloat(s[6], 64)

				if !strings.Contains(only, ","+league+",") || strings.Contains(league, "杯") {
					return nil
				}
				boltdb.Get("lastmatch-2017", league, string(k), func(text string) {
					//去掉没有联赛的比赛
					if !strings.Contains(text, "总") || !strings.Contains(text, "BaseInfo") {
						return
					}

					overunder.Format(string(k), league, startAt, t1, t2, text)
					w := 0

					if (s1 + s2 ) >= 3 {
						w = 1
					}

					slice := []interface{}{string(k), w, s1, s2,}
					fmt.Println(i)
					fn(slice, i)

				})
				return nil
			})
			return nil
		})
	})
}

func main()  {
	e:=[]string{"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"}
	xlsx := excelize.NewFile()

	categories := []string{

	}
	for k,v:=range categories{
		len:=int(k/26)
		s:=""
		if len>0{
			s+=e[len-1]+e[k%26]
		}else{
			s+=e[k%26]
		}

		xlsx.SetCellValue("Sheet1",s+"1", v)
	}
	overunderScore(func(s []interface{}, i int) {
		for k,v:=range s{
			len:=int(k/26)
			s:=""
			if len>0{
				s+=e[len-1]+e[k%26]
			}else{
				s+=e[k%26]
			}
			xlsx.SetCellValue("Sheet1",s+strconv.Itoa(i+1), v)
		}
	})
	err := xlsx.SaveAs("./out/overunder.xlsx")
	if err != nil {
		fmt.Println(err)
	}

}

